<?php
namespace Tbf\Crypto;
/**
 * 使用mcrypt加密解密
 * 默认padding方式为多余字节加\x00  会吃掉最后的\x00
 * 修改为openssl方式
 * @deprecated
 */
class McryptCrypto extends Crypto{
    static $cipherTable = array(
            'aes-256'=>'rijndael-128');
    static $cipherInfo = array (
            'rijndael-128' =>
            array (
                    'blockSize' => 16,
                    'keySize' => 32,
                    'ivSize' => 16,
            ),
            'rijndael-256' =>
            array (
                    'blockSize' => 32,
                    'keySize' => 32,
                    'ivSize' => 32,
            ),
    );
    static function canLoad(){
        return function_exists('mcrypt_encrypt');
    }
    static function getCipherList(){
        return mcrypt_list_algorithms();
    }
    function dHash($data,$hashType){
        return tPanic('not implement',get_class($this));
    }
    function dCipherEn($data, $cipherType, $mode, $key, $iv){
        $cipher = self::$cipherTable[$cipherType];
        $block = $this->cipherBlockSize($cipherType);
        $pad = $block - (strlen($data) % $block);
        $data .= str_repeat(chr($pad), $pad);
        return mcrypt_encrypt($cipher,$key,$data,$mode,$iv);
    }
    function dCipherDe($data, $cipherType, $mode, $key, $iv){
        $cipher = self::$cipherTable[$cipherType];
        $data = mcrypt_decrypt($cipher,$key,$data,$mode,$iv);
        $len = strlen($data);
        $pad = ord($data[$len - 1]);
        $data = substr($data, 0, $len - $pad);
        return $data;
    }
    function cipherIvSize($cipherType){
        $cipher = self::$cipherTable[$cipherType];
        return self::$cipherInfo[$cipher]['ivSize'];
    }
    function cipherKeySize($cipherType){
        $cipher = self::$cipherTable[$cipherType];
        return self::$cipherInfo[$cipher]['keySize'];
    }
    function cipherBlockSize($cipherType){
        $cipher = self::$cipherTable[$cipherType];
        return self::$cipherInfo[$cipher]['blockSize'];
    }
    static function dObj(){
        static $cache= null;
        if ($cache===null){
            $cache = new static;
        }
        return $cache;
    }
}

